package com.ljt.framework.test;

import static org.mockito.Matchers.any;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.ljt.framework.test.demo.StaticUserService;
import com.ljt.framework.test.demo.UserAction;

/**
 * @Project       : framework-test
 * @Program Name  : com.ljt.framework.test.MockForPrivateStaticDemo.java
 * @Description   : 测试含有调用私有静态方法的场景
 * @Author        : wangchao
 * @Creation Date : 2016年4月14日 上午11:32:37 
 * @ModificationHistory  
 * Who          When             What 
 * ----------   -------------    -----------------------------------
 * wangchao     2016年4月14日        create
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest({StaticUserService.class,UserAction.class})
public class MockForPrivateStaticDemo {
	
	@Test
	public void test() throws Exception{
		PowerMockito.spy(StaticUserService.class);
		// 模拟静态私有且有返回值的方法
		PowerMockito.doReturn("mock").when(StaticUserService.class,"secreteSayHello",any(String.class));
		// 模拟静态私有无返回值的方法
		PowerMockito.doNothing().when(StaticUserService.class,"secreteSayHi",any(String.class));
		
		// 执行业务方法
		UserAction userAction = new UserAction();
		userAction.executeForPrivateStatic("real");
		userAction.executeForPrivateStatic("real");
		
		// 验证私有方法被调用了2次
		PowerMockito.verifyPrivate(StaticUserService.class,Mockito.times(2)).invoke("secreteSayHello",any(String.class));
		PowerMockito.verifyPrivate(StaticUserService.class,Mockito.times(2)).invoke("secreteSayHi",any(String.class));
	}
	
}
